package com.solace.selenium.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.solace.selenium.entity.VideoGroup;
import com.solace.selenium.entity.VideoOps;
import com.solace.selenium.mapper.VideoGroupMapper;
import com.solace.selenium.service.VideoConfService;
import com.solace.selenium.service.VideoGroupService;
import com.solace.selenium.service.VideoOpsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class VideoGroupServiceImpl extends ServiceImpl<VideoGroupMapper, VideoGroup> implements VideoGroupService {
    @Autowired
    private VideoOpsService videoOpsService;
    @Autowired
    private VideoConfService videoConfService;

    @Override
    public void checkData(List<VideoGroup> list) {
        if (!CollectionUtils.isEmpty(list)){
            for (VideoGroup videoGroup : list) {
                Assert.hasText(videoGroup.getName(),"分组名不能为空");
            }
        }
    }

    @Override
    public void addVideoGroup(String name, String note, List<VideoOps> opsList) {
        //创建分组
        Assert.hasText(name,"分组名字不能为空");
        //写入操作
        VideoGroup videoGroup = new VideoGroup();
        videoGroup.setName(name);
        videoGroup.setNote(note);
        save(videoGroup);
        //分组ID
        for (VideoOps videoOps : opsList) {
            videoOps.setGroupId(videoGroup.getId());
        }
        //检查
        videoOpsService.checkData(opsList);
        videoOpsService.saveBatch(opsList);
    }

    @Override
    public List<VideoGroup> findAll() {
        LambdaQueryWrapper<VideoGroup> wrapper = new QueryWrapper<VideoGroup>()
                .lambda()
                .orderByAsc(VideoGroup::getSort);
        return list(wrapper);
    }

    @Override
    public JSONObject findByIdAndGroupName(String groupId) {
        JSONObject js = new JSONObject();
        VideoGroup byId = getById(groupId);
        if (null!=byId){
            js.put("group", byId);
            List<VideoOps> byGroupId = videoOpsService.findByGroupId(byId.getId());
            js.put("ops", byGroupId);
        }
        return js;
    }

    @Override
    public IPage<VideoGroup> findList(Long current, Long pageSize) {
        LambdaQueryWrapper<VideoGroup> wrapper = new QueryWrapper<VideoGroup>()
                .lambda()
                .orderByAsc(VideoGroup::getSort);
        Page<VideoGroup> page = new Page<>(current, pageSize);
        return page(page,wrapper);
    }

    @Override
    public void delGroup(String groupId) {
        //删除分组
        removeById(groupId);
        //删除ops
        videoOpsService.delByGroup(groupId);
        //删除配置
        videoConfService.delByGroup(groupId);
    }

    @Override
    public void updateVideoGroup(String groupId, String name, String note, List<VideoOps> opsList) {
        Assert.hasText(groupId,"分组ID不能为空");
        VideoGroup group = getById(groupId);
        Assert.notNull(group,"查询分组信息失败");
        if (StringUtils.hasText(name)){
            group.setName(name);
        }
        if (StringUtils.hasText(note)){
            group.setNote(note);
        }
        updateById(group);
        for (VideoOps videoOps : opsList) {
            videoOps.setGroupId(groupId);
        }
        videoOpsService.checkData(opsList);
        //删除所有操作
        videoOpsService.delByGroup(groupId);
        videoOpsService.saveBatch(opsList);
    }

    @Override
    public void copyByIdVideoGroup(String groupId) {
        VideoGroup videoGroup = getById(groupId);
        Assert.notNull(videoGroup,"未获取到分组信息");
        VideoGroup group = new VideoGroup();
        BeanUtil.copyProperties(videoGroup,group);
        group.setId(null);
        group.setName(group.getName()+"COPY");
        save(group);
        //ops
        List<VideoOps> groupOpsList = videoOpsService.findByGroupId(groupId);
        List<VideoOps> copyOpsList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(groupOpsList)){
            for (VideoOps videoOps : groupOpsList) {
                VideoOps ops = new VideoOps();
                BeanUtil.copyProperties(videoOps,ops);
                ops.setId(null);
                ops.setGroupId(group.getId());
                copyOpsList.add(ops);
            }
            videoOpsService.saveBatch(copyOpsList);
        }
    }
}